package com.service.impl;

import com.dao.GroupMessageDao;
import com.dao.GroupUserDao;
import com.entity.GroupMessage;
import com.entity.GroupUser;
import com.service.GroupUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class GroupUserServiceImpl implements GroupUserService {
    @Autowired
    private GroupUserDao groupUserDao;
    @Autowired
    private GroupMessageDao groupMessageDao;

    @Override
    public GroupUser getById(Integer id) {
        return groupUserDao.getById(id);
    }

    @Override
    @Transactional
    public List<GroupUser> getGroupUserByUserId(Integer userId) {
        List<GroupUser> groupUsers = groupUserDao.getGroupUserByUserId(userId);
        for (GroupUser groupUser : groupUsers) {
            //获得该群最新消息
            GroupMessage newGroupMessage = groupMessageDao.getNewGroupMessageByGroupId(groupUser.getGroupInfo().getId());
            //设置该群最新消息
            groupUser.getGroupInfo().setNewGroupMessage(newGroupMessage);
        }
        return groupUsers;
    }

    @Override
    public List<GroupUser> getAllByGroupId(Integer groupId) {
        return groupUserDao.getAllByGroupId(groupId);
    }

    @Override
    public Integer getNotReadCount(Integer userId, Integer groupId) {
        return groupUserDao.getNotReadCount(userId, groupId);
    }

    @Override
    public void editNotReadCount(Integer notReadCount,Integer userId, Integer groupId) {
        groupUserDao.editNotReadCount(notReadCount,userId,groupId);
    }

    //添加
    @Override
    public Boolean addGroupUser(Integer userId, Integer groupInfoId) {
        GroupUser groupUser = new GroupUser();
        groupUser.setUserId(userId);
        groupUser.setGroupId(groupInfoId);
        return groupUserDao.addGroupUser(groupUser);
    }

    //修改
    @Override
    public Boolean editGroupUser(GroupUser groupUser) {
        return groupUserDao.editGroupUser(groupUser);
    }
    //群主转让
    @Transactional
    @Override
    public Boolean editRoleTransfer(Integer groupUserIdMaster, Integer groupUserIdServant) {
        GroupUser groupUser = new GroupUser();
        //群主变为管理员
        groupUser.setId(groupUserIdMaster);
        groupUser.setGroupRole(1);
        groupUserDao.editGroupUser(groupUser);
        //使成员变成新群主
        groupUser.setId(groupUserIdServant);
        groupUser.setGroupRole(0);
        return groupUserDao.editGroupUser(groupUser);
    }

    //移出该群
    @Override
    @Transactional
    public Boolean delByIdGroupUser(Integer groupUserId) {
        GroupUser groupUser = groupUserDao.getById(groupUserId);
        Boolean resultBool = false;
        if(groupUser!=null){
            groupUser.setIsRemove(1);
            groupUser.setNoteName("");
            groupUser.setNotReadCount(0);
            groupUser.setGroupRole(2);
            groupUser.setIsSilent(0);
            resultBool = groupUserDao.editGroupUser(groupUser);
        }
        return resultBool;
    }
}
